320B - Ping-Pong (Easy Version) - CodeForces Solution


dfs and similar graphs *1500

Please click on ads to support us..

Python Code:

def query_add(x, y, intervals):
    intervals.append((x, y))


def can_move(a, b, c, d):
    return c < a < d or c < b < d


def query_has_path(param1, param2, intervals):
    stack = [param1]
    visited = [False] * len(intervals)
    visited[param1 - 1] = True
    while len(stack) > 0:
        cur = stack.pop()
        a, b = intervals[cur - 1]
        for i in range(0, len(intervals)):
            if visited[i]:
                continue
            c, d = intervals[i]
            if can_move(a, b, c, d):
                if i + 1 == param2:
                    print("YES")
                    return
                visited[i] = True
                stack.append(i + 1)
    print("NO")


def parse_queries(num_queries):
    queries = []
    intervals = []
    for i in range(0, num_queries):
        queries.append(input().split())
    for t, param1, param2 in queries:
        if t == "1":
            query_add(int(param1), int(param2), intervals)
        else:
            query_has_path(int(param1), int(param2), intervals)


if __name__ == '__main__':
    n = int(input())
    parse_queries(n)

C++ Code:

#include <bits.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <string.h>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#define ll long long

using namespace std;
const int N = 1e5 + 7, INF =1e9 + 7;
int n,m;
map<int,vector<pair<int,int>>>adj;    // *
map<int,int>vis;
bool ans =false;
void dfs(int st,pair<int,int> dis){
    vis[st] =1;
    for(auto child: adj[st]){
        if(child.first == dis.first && child.second == dis.second){ans =true;return;}
        if(!vis[child.first]){dfs(child.first,dis);}
    }
}
void init(){for(auto &i:vis){i.second =0;}}
void solve(){
    cin >> n;
    vector<pair<int,int>>input(n+10);
    int type,c,d;
    for(int i=0,j=1;i<n;i++){
        cin >> type >> c >> d;
        if(type == 1){
            for(auto pr: input){
                if(pr.first > c && pr.first < d || pr.second > c && pr.second < d){
                    adj[pr.first].push_back({c,d});
                }
                if(pr.first < c && pr.second > c || pr.first < d && pr.second > d){
                    adj[c].push_back(pr);
                }
            }
            vis[c] =0;
            vis[d] =0;
            input[j++] = {c,d};
        }
        else{
            init();
            dfs(input[c].first,input[d]);
            if(ans) cout << "YES\n";
            else cout << "NO\n";
            ans = false;
        }
    }
}
int main(){
    int t;
    t =1;
    while(t--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks